﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class SiteCfgMan : xIx.Web.UI.PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) {
            DataBind();
        }
    }
    protected void ResetButton_Click(object sender, ImageClickEventArgs e)
    {
        DataBind();
    }
    protected void SaveButton_Click(object sender, ImageClickEventArgs e)
    {
        DataTable dt = null;

        using (xIx.Data.DbHelper dh = new xIx.Data.DbHelper())
        {
            dt = dh.Table["Sys_Configs"].Where("CfgClass='SiteCfg'").OrderBy("SortIdx").Select();
            dt.PrimaryKey = new DataColumn[] { dt.Columns["ID"] };
            int ec = 0;
            foreach (GridViewRow gr in GridView1.Rows)
            {
                if (gr.RowType == DataControlRowType.DataRow) {
                    DataRow dr = dt.Rows.Find(GridView1.DataKeys[gr.RowIndex].Value);
                    if (null != dr) { 
                        string nv=(gr.Cells[1].FindControl("TxtCfgVal") as TextBox).Text; 
                        if(!nv.Equals(dr["CfgVal"])){
                            try {
                                dh.Table["Sys_Configs"].Fields("CfgVal").UpdateWith(nv).Where("ID=@RID").Exec(dr["ID"]);
                            }
                            catch {
                                ec++;
                            }
                        }
                    }
                }
            }
            if (ec > 0) {
                Alert("保存时遇到错误。");
            }
        }

        DataBind();
    }

    protected override void OnDataBinding(EventArgs e)
    {
        DataTable dt = null;
        using (xIx.Data.DbHelper dh = new xIx.Data.DbHelper()) { 
            dt=dh.Table["Sys_Configs"].Where("CfgClass='SiteCfg'").OrderBy("SortIdx").Select();
        }
        GridView1.DataSource = dt;
        base.OnDataBinding(e);
    }
}
